﻿using Artech.Common;
using Microsoft.Practices.Unity.Utility;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Artech.Core.Resources
{
    [DataContract(Namespace = Constants.Namespaces.DataContract)]
    public class StringResourceSearchCriteria
    {
        [DataMember]
        private IList<string> cultureNames;

        [DataMember]
        public string Category { get; set; }

        [DataMember]
        public string Name { get; set; }
        
        public IList<CultureInfo> Cultures { get; private set; }

        [OnSerializing]
        public void OnSerializing(StreamingContext context)
        {
            cultureNames.Clear();
            foreach(CultureInfo culture in this.Cultures)
            {
                cultureNames.Add(culture.Name);
            }
        }

        [OnDeserialized]
        public void OnDeserialized(StreamingContext context)
        {
            this.Cultures.Clear();
            foreach (string culture in cultureNames)
            {
                this.Cultures.Add(new CultureInfo(culture));
            }
        }
        

        public static StringResourceSearchCriteria NewCriteria(string category, string name, params CultureInfo[] cultures)
        {
            cultures = cultures ?? new CultureInfo[0];
            return new StringResourceSearchCriteria
            {
                Category = category,
                Name = name,
                Cultures = new List<CultureInfo>(cultures)
            };
        }

        public StringResourceSearchCriteria()
        {
            this.Cultures = new List<CultureInfo>();
            cultureNames = new List<string>();
        }
    }
}